package com.dat255.tentatime;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import com.dat255.internalstorage.*;
public class ChangeExamActivity extends Activity {
	
	private EditText courseid, coursename, courseplace;
	private DatePicker datepicker;
	private TimePicker timepicker;
	private Button buttonSave, buttonCancel;
	private InternalStorage fav = new InternalStorage(this);
	private Exam oldExam;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exam_form);
		
		 //Assign components
        courseid = (EditText) findViewById(R.id.formCourseID);
        coursename = (EditText) findViewById(R.id.formCourseName);
        courseplace = (EditText) findViewById(R.id.formPlace);
        
        datepicker = (DatePicker) findViewById(R.id.datePicker);
        timepicker = (TimePicker) findViewById(R.id.timePicker);
        
        buttonSave = (Button) findViewById(R.id.buttonSave);
        buttonCancel = (Button) findViewById(R.id.buttonCancel);
        
        
        buttonSave.setOnClickListener(saveListener);
        buttonCancel.setOnClickListener(cancelListener);
        
      
        
        
        //Setvalues to the input form
        
        Bundle data = getIntent().getExtras();
        
        oldExam = data.getParcelable("changeexam");
        
        courseid.setText(oldExam.getCourseID());
        coursename.setText(oldExam.getCourseName());
        courseplace.setText(oldExam.getPlace());
     
       
        
	}
	


	//Create an anonymous implementation of OnClickListener
    private OnClickListener saveListener = new OnClickListener() {
        
    	
    	public void onClick(View v) {
       
        	
        	
        	String time = timepicker.getCurrentHour().toString() + ":" + timepicker.getCurrentMinute();
        	String date = datepicker.getDayOfMonth() + "-" + (datepicker.getMonth()+1) + "-" + datepicker.getYear();
        	//TODO: validate the input
        	
        	Exam newExam = new Exam(courseid.getText().toString(), coursename.getText().toString(),time, date, courseplace.getText().toString());
        	
        	if(fav.changeExam(newExam, oldExam.getCourseID())){
        		
        		
        		Intent examIntent = new Intent(ChangeExamActivity.this, FavouriteActivity.class);
    			ChangeExamActivity.this.startActivity(examIntent);	
    			finish();
        		
        		
        	}
        	
        	
        	
        	
        	//TODO: store the information on the server
        	//TODO: Show message to the admin whether it was successful or not
        	
                       
      
        }
    };
     
    // Create an anonymous implementation of OnClickListener
    private OnClickListener cancelListener = new OnClickListener() {
        public void onClick(View v) {
        	
        	
        	//Kill the activity
        	finish();
        	
        	
    
        } 
    };

}
